package com.app.xinfadi.dao;

import android.graphics.Bitmap;
import android.util.Base64;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.base.Constant;
import com.app.xinfadi.base.MyApplication;
import com.app.xinfadi.dao_bean.SerialNumberBean;
import com.app.xinfadi.dao_bean.UserImageBean;
import com.app.xinfadi.dao_bean.UserLoginBean;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.widget.gson.GsonRequest;
import com.app.xinfadi.widget.gson.Success;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 *
 * @author WuTengFei <br/>
 * @Function: TODO <br/>
 * @date 2015-5-31 上午10:05:19 <br/>
 */
public class UserDao {
    private final static String TAG = UserDao.class.getSimpleName();

    public UserDao() {

    }
    /**
     * 登陆
     *
     * @param text_mobile   手机号
     * @param text_password 密码
     * @param listener
     */
    public void login(final String text_mobile, final String text_password, Response.Listener<UserLoginBean> listener, Response.ErrorListener errorListener) {
        String URL = Constant.URL + "userService/login";
        GsonRequest<UserLoginBean> gsonRequest = new GsonRequest<UserLoginBean>(Request.Method.POST, URL,
                UserLoginBean.class, listener, errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", text_mobile);
                params.put("password", text_password);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 获取验证码
     * 注册步骤
     *
     * @param phoneNumber
     * @param listener
     */
    public void getSerialNumber(final String phoneNumber, final Response.Listener<SerialNumberBean> listener, Response.ErrorListener errorListener) {
        String URL = Constant.URL + "register/get_serial_number";
        GsonRequest<SerialNumberBean> gsonRequest = new GsonRequest<SerialNumberBean>(Request.Method.POST, URL,
                SerialNumberBean.class, listener, errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("phone_number", phoneNumber);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 获取验证码
     * 忘记密码 步骤（不做注册人处理）
     *
     * @param phoneNumber
     * @param listener
     */

    public void getForgetPasswordSerialNumber(final String phoneNumber, Response.Listener<SerialNumberBean> listener) {
        String URL = Constant.URL + "user/get_serial_number";
        GsonRequest<SerialNumberBean> gsonRequest = new GsonRequest<SerialNumberBean>(Request.Method.POST, URL,
                SerialNumberBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtil.e("TAG", error.toString());
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("phone_number", phoneNumber);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 注册账号
     *
     * @param nickname
     * @param password
     * @param phoneNumber
     * @param sex
     * @param listener
     */
    public void registerAccount(final String nickname, final String password, final String phoneNumber, final String sex, Response.Listener<Success> listener) {
        String URL = Constant.URL + "register/register_user";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL, Success.class,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtil.e("TAG", error.toString());
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_name", nickname);
                params.put("password", password);
                params.put("phone_number", phoneNumber);
                params.put("user_sex", sex);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 改变性别
     *
     * @param sex
     * @param listener
     * @param errorListener
     */
    public void changeSex(final String sex, Response.Listener<Success> listener, Response.ErrorListener errorListener) {
        LogUtil.d(TAG, "改变性别:" + sex);
        String URL = Constant.URL + "user/change_sex";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL, Success.class, listener,
                errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("sex", sex);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 更改用户名
     *
     * @param name
     */
    public void changeNickName(final String name, Response.Listener<Success> listener,
                               Response.ErrorListener errorListener) {
        String URL = Constant.URL + "user/change_name";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL, Success.class, listener,
                errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_name", name);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 更改密码
     *
     * @param oldPassword
     * @param password
     */
    public void changePassword(final String oldPassword, final String password, Response.Listener<Success> listener,
                               Response.ErrorListener errorListener) {
        String URL = Constant.URL + "user/change_password";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL, Success.class, listener,
                errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("oldpassword", oldPassword);// 新版Api删除
                params.put("password", password);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    public void changeUserImage(final Bitmap bitmap, Response.Listener<UserImageBean> listener) {
        String URL = Constant.URL + "user/change_user_image";
//		String URL = Constant.IMAGE_IN_URL;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式，100：压缩率，baos：字节流
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] buffer = baos.toByteArray();
        LogUtil.e(TAG, "图片的大小：" + buffer.length);
        // 将图片的字节流数据加密成base64字符输出
        final String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        GsonRequest<UserImageBean> gsonRequest = new GsonRequest<UserImageBean>(Request.Method.POST, URL, UserImageBean.class,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtil.e("TAG", error.toString() + "");
                LogUtil.e("LOGIN-ERROR_1", error.getMessage());
                // byte[] htmlBodyBytes = error.networkResponse.data;
                // LogUtil.e("LOGIN-ERROR_2", new
                // String(htmlBodyBytes));
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("image_data", photo);
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 忘记密码
     *
     * @param password
     * @param listener
     * @param errorListener
     */
    public void forgetPassword(final String password, final String phone_number, Response.Listener<Success> listener, Response.ErrorListener errorListener) {
        String URL = Constant.URL + "user/forget_password";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL, Success.class, listener,
                errorListener) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("password", password);
                params.put("phone_number", phone_number);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }
}
